package com.google.gwt.sample.dynatablerf.server.importengine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.google.gwt.sample.dynatablerf.server.util.ConvertUtil;

public class ImportEngineUtil {

	private static InputStream getInputStream(String entryName, ZipFile zipFile)
			throws IOException {
		if (zipFile != null && entryName != null)
			return zipFile.getInputStream(new ZipEntry(entryName));
		else
			return null;
	}

	private static Iterator<Row> getCellIterator(InputStream inputStream,
			int sheetNumber) throws InvalidFormatException, IOException {
		OPCPackage opcPackage = OPCPackage.open(inputStream);
		XSSFWorkbook wb = new XSSFWorkbook(opcPackage);
		XSSFSheet sheet = wb.getSheetAt(sheetNumber);
		inputStream.close();
		return sheet.rowIterator();
	}

	public static Iterator<Row> getCellIterator(String xlsFileName,
			int sheetNumber, ZipFile zipFile) throws IOException,
			InvalidFormatException {
		InputStream inputStream = getInputStream(xlsFileName, zipFile);
		if (inputStream != null)
			return getCellIterator(inputStream, sheetNumber);
		else
			return null;
	}

	public static String getCellStringValue(Row row, int cellIndex) {
		String resultado = null;
		XSSFRow xssfRow = (XSSFRow) row;
		XSSFCell cell = xssfRow.getCell(cellIndex);
		if (cell != null) {
			if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
				resultado = ConvertUtil.getParteInteira(cell
						.getNumericCellValue());
			} else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
				resultado = cell.getStringCellValue().trim();
			}
		}
		return resultado;
	}

	public static BufferedReader getBufferedReader(ZipFile zipFile,
			String zipEntry) throws IOException {
		return new BufferedReader(new InputStreamReader(
				zipFile.getInputStream(new ZipEntry(zipEntry)), Charset.forName("ISO8859-1")));
	}
	
	public static String readZipEntryLine(BufferedReader bufferedReader) throws IOException {
		String resultado = null;
		try {
			resultado = bufferedReader.readLine();
		} catch(Exception e) {
			bufferedReader.close();
		}
		return resultado;
	}
	
	public static String getDataPerSeparator(String line, char separator, int position) {
		String result = "";
		int index = 0;
		char c;
		for (int i = 0; i <= position; i++) {
			result = "";
			while ((c = line.charAt(index++)) != separator) {
				result += c;
			}
		}
		return result.trim();
	}
	
	public static String getDataByLength(String line, int start, int length) {
		return getData(line, start, start + length);
	}
	
	public static String getData(String line, int start, int end) {
		if(line != null)
			return line.substring(start, end).trim();
		else
			return null;
	}
	
	public static int getNumberOfLines(BufferedReader bufferedReader) throws IOException {
		LineNumberReader  lnr = new LineNumberReader(bufferedReader);
		lnr.skip(Long.MAX_VALUE);
		return lnr.getLineNumber();
	}
}
